package com.assessment.service.impl;

import com.assessment.entity.*;
import com.assessment.mapper.*;
import com.assessment.service.AdepService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.enums.SqlMethod;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import static com.assessment.utils.Constant.BATCH_COUNT;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 鑫十一
 * @since 2021-12-15
 */
@Service
@Transactional
public class AdepServiceImpl extends ServiceImpl<AdepMapper, Adep> implements AdepService {

    private static final Logger log1 = LoggerFactory.getLogger(AdepServiceImpl.class);

    @Autowired(required = false)
    private AroleMapper aroleMapper;
    @Autowired(required = false)
    private ApeoMapper apeoMapper;
    @Autowired(required = false)
    private AitemavgMapper aitemavgMapper;
    @Autowired(required = false)
    private AscoreMapper ascoreMapper;
    @Autowired(required = false)
    private AcomresMapper acomresMapper;


    @Override
    public List<Column> getColumns() {
        List<Column> columns = new ArrayList<>();
        columns.add(new Column().setLabel("部门名").setProp("name"));
        columns.add(new Column().setLabel("部门角色").setProp("rname"));
        columns.add(new Column().setLabel("部门位次").setProp("dsit"));
        return columns;
    }

    @Override
    public Boolean checkDep(Adep data) {
        // 部门名是不是重复
        QueryWrapper<Adep> adepQueryWrapper = new QueryWrapper<>();
        adepQueryWrapper.eq("name", data.getName());
        List<Adep> list = this.list(adepQueryWrapper);
        if (list.size() > 0) { return false; }

        // 角色是不是存在
        QueryWrapper<Arole> aroleQueryWrapper = new QueryWrapper<>();
        aroleQueryWrapper.eq("name", data.getRname());
        List<Arole> aroles = aroleMapper.selectList(aroleQueryWrapper);
        if (aroles.size() <= 0){ return false; }

        data.setRid(aroles.get(0).getId());
        data.setRtype(aroles.get(0).getType());
        data.setRsit(aroles.get(0).getRsit());

        return true;
    }

    @Override
    public boolean updateDep(Adep item) {
        Apeo apeo = new Apeo();
        apeo.setDname(item.getName());
        apeo.setDrole(item.getRname());
        apeo.setDsit(item.getDsit());
        apeoMapper.update(apeo,new QueryWrapper<Apeo>().eq("did",item.getId()));

        Acomres acomres = new Acomres().setPname(item.getName());
        acomresMapper.update(acomres,new QueryWrapper<Acomres>().eq("pid",item.getId()));

        Ascore ascore = new Ascore().setBpname(item.getName())
                .setBrsit(item.getRsit()).setDsit(item.getDsit())
                .setBrole(item.getRname()).setBrtype(item.getRtype()).setBrid(item.getRid())
                .setFurl(item.getFurl()).setFname(item.getFname());
        ascoreMapper.update(ascore,new QueryWrapper<Ascore>().eq("bpid",item.getId()));

        Aitemavg aitemavg = new Aitemavg().setPname(item.getName()).setProle(item.getRname()).setPrid(item.getRid())
                .setPrtype(item.getRtype()).setPsit(item.getDsit());
        aitemavgMapper.update(aitemavg,new QueryWrapper<Aitemavg>().eq("pid",item.getId()));

        return this.updateById(item);
    }
}
